﻿using DapperExtensions;
using Restaurant.Common;
using Restaurant.Dao;
using Restaurant.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Restaurant.Business
{
    /// <summary>
    /// 桌子Business
    /// </summary>
    public class TableBusiness
    {
        readonly TableDao tableDao = new TableDao();

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="table"></param>
        /// <returns></returns>
        public async Task<OperationResult> AddAsync(CompanyTable table)
        {
            var r = await tableDao.SaveAsync(table);

            if (null == r)
                return new OperationResult(OperationResultType.NoChanged, "没有发生变化");

            return new OperationResult(OperationResultType.Success, "成功", r);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="table"></param>
        /// <returns></returns>
        public async Task<OperationResult> DeleteAsync(long id)
        { 
            var r = await tableDao.RemoveAsync(id);

            if (false == r)
                return new OperationResult(OperationResultType.Error, "删除失败");

            return new OperationResult(OperationResultType.Success, "成功", r);

        }

        /// <summary>
        /// 获取分公司下桌子
        /// </summary>
        /// <param name="subCompanyId">分公司Id</param>
        /// <returns></returns>
        public async Task<OperationResult> GetAllBySubComanyId(Guid subCompanyId)
        {

            //where
            IList<IPredicate> predList = new List<IPredicate>();
            predList.Add(Predicates.Field<CompanyTable>(p => p.SubCompany_Id, Operator.Eq, subCompanyId));
            IPredicateGroup predGroup = Predicates.Group(GroupOperator.And, predList.ToArray());

            var r = await tableDao.GetListAsync(predGroup);


            if (null == r)
                return new OperationResult(OperationResultType.QueryNull, "数据不存在");

            return new OperationResult(OperationResultType.Success, "成功", r);
        }


    }
}
